export function _debounce(fn, delay) {
    //防抖
    var delay = delay || 200;
    var timer;
    return function () {
        var th = this;
        var args = arguments;
        if (timer) {
            clearTimeout(timer);
        }
        timer = setTimeout(function () {
            timer = null;
            fn.apply(th, args);
        }, delay);
    };
}
// 节流
export function _throttle(fn, interval) {
    var last;
    var timer;
    var interval = interval || 200;
    return function () {
        var th = this;
        var args = arguments;
        var now = +new Date();
        if (last && now - last < interval) {
            clearTimeout(timer);
            timer = setTimeout(function () {
                last = now;
                fn.apply(th, args);
            }, interval);
        } else {
            last = now;
            fn.apply(th, args);
        }
    }
}
// yyyy-mm-dd hh:mm:ss
export function _formatTime(date) {
    var date = new Date(date);
    var YY = date.getFullYear() + '-';
    var MM = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    var DD = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate());
    var hh = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
    var mm = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
    var ss = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
    return YY + MM + DD + " " + hh + mm + ss;
}
// yyyy/mm/dd 
export function _formatYMD(date) {
    var date = new Date(date);
    var YY = date.getFullYear() + '/';
    var MM = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '/';
    var DD = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate());
    return YY + MM + DD;
}
// getUrlKey 
export function _getUrlKey(name, url) {
    return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(url) || [, ""])[1].replace(/\+/g, '%20')) || null
}

/**
 * silas
 * 年，月，星期    
 */
/**
 *  是否是闰年
 * @param {*} year   
 * @returns true/false boolean
 */
export function isLeapYear(year) {
    return year % 400 === 0 || (year % 100 !== 0 && year % 4 === 0)
}

/**
 * @param year xxxx
 * @param month xx 
 * @return counts number
 * remark +month - 1 in function  
 */
export function getMonthDays(year, month) {
    var arr = [31, null, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
        count = (arr[+month - 1]) || (isLeapYear(year) ? 29 : 28);

    return count;
}

/**
 * @param dateString xxxx-xx-xx
 * @return 星期* string
 */

export function getWeek(dateString) {
    var date, dateArray = dateString.split("-");
    date = new Date(dateArray[0], parseInt(dateArray[1] - 1), dateArray[2]);
    return "星期" + "日一二三四五六".charAt(date.getDay());
}


/**
* 数字转成汉字
* @params num === 要转换的数字
* @return 汉字
* */
export function toChinesNum(num) {
    num = parseInt(num)
    if(isNaN(num)) { return };
    let changeNum = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九' , '十'];
    // let unit = ['', '十',];

    if(num <= 10){ return changeNum[num] };
    let numStr = num.toString();
    let tens = numStr.slice(0, 1), //十位
        single = numStr.slice(1, 2); //个位
    
    let tensStr;
    if(tens == 1){
        tensStr = "十";
    } else {
        tensStr = changeNum[+tens] + "十"
    }
    let singleStr = single == 0 ? "" : changeNum[+single];
    return tensStr + singleStr;
}